from decimal import Decimal

def simple_balance(arr, s2):
	out = [0 for el in arr]
	s = 0
	while s < s2:
		for i in range(0, len(arr)):
			if (out[i] < arr[i]):
				out[i] += 1
				s += 1
			if s >= s2:
				break
	return out

def cmp2(a,b):
	return cmp(a[2], b[2])
def cmp0(a,b):
	return cmp(a[0], b[0])

def proportional_balance(arr, s2):
	s2 = Decimal(str(s2))
	r = Decimal(s2 / sum(arr))
	out = [int(Decimal(str(a)) * r) for a in arr]
	s3 = sum(out)
	tplr = [[i, out[i], arr[i]] for i in range(0, len(out))]
	tplr.sort(cmp2, reverse=True)
	for tpl in tplr:
		#if tpl[1] < arr[tpl[0]] and s3 < s2:
		if s3 < s2:
			tpl[1] += 1
			s3 += 1
	tplr.sort(cmp0)
	return [t[1] for t in tplr]


